<?php

namespace App\Http\Controllers\Api;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Redis;
use App\Services\WorkService;
use App\Services\WorkcustomService;
use App\Services\WorkhanddleService;
use App\Services\WorkcontactService;
use App\Services\WorkkefuService;
use App\Services\MemberService;
use App\Models\Workcorps;
use Log,Exception;

class WorkttwkController extends Controller
{
    public function instructBack(Request $request)
    {
        if (empty($request->msg_signature)) return "";
        if (isset($request->echostr)) {
            return WorkService::msgDecryptCorp($request);
        }
        $message = WorkService::msgDecryptSuit($request);
        if($message["InfoType"] == "error") return "error";
        switch ($message['InfoType']) {
            case 'suite_ticket':
                Redis::setex("open_work_suite_ticket_".$message['SuiteId'],1800,$message['SuiteTicket']);
                break;
            case 'cancel_auth':
                WorkhanddleService::cancelAuth($message);
                break;
            case 'create_auth':
                WorkhanddleService::createAuth($message);
                break;
            case 'change_auth':
                WorkhanddleService::changeAuth($message);
                break;
            case 'change_external_contact':
                // 通讯录更新
                if ($message["ChangeType"] == "add_external_contact") {
                    // 添加微信好友
                    WorkcontactService::addExternalContact($message);
                } elseif ($message["ChangeType"] == "del_follow_user") {
                    // 删除微信好友
                    WorkcontactService::delExternalContact($message);
                }
                break;
            case 'customer_acquisition':
                // 获客助手相关回传
                if ($message["ChangeType"] == "customer_start_chat") {
                    // 微信客户发起会话事件
                    WorkcontactService::customerStartChat($message);
                } elseif ($message["ChangeType"] == "balance_low") {
                    // 获客额度即将耗尽事件
                } elseif ($message["ChangeType"] == "balance_exhausted") {
                    // 获客额度已经耗尽事件
                } elseif ($message["ChangeType"] == "link_unavailable") {
                    // 获客链接失效事件
                } elseif ($message["ChangeType"] == "delete_link") {
                    // 删除获客链接事件
                } elseif ($message["ChangeType"] == "quota_expire_soon") {
                    // 获客额度即将过期事件
                } elseif ($message["ChangeType"] == "friend_request") {
                    // 通过获客链接申请好友事件
                    WorkcontactService::customerFriendRequest($message);
                }
                break;
            case 'cancel_order':
                // 取消订单通知
                MemberService::cancelOrder($message);
                break;
            case 'refund':
                // 退款通知
                MemberService::refundOrder($message);
                break;
            case 'pay_for_app_success':
                Log::info("instructBack--------");
                Log::info($message);
                Log::info("instructBack--------");
                // 支付成功通知
                MemberService::paySuccessOrder($message);
                break;
            case 'change_order':
                Log::info("instructBack--------");
                Log::info($message);
                Log::info("instructBack--------");
                // 改单通知
                MemberService::refundOrder($message);
                break;
            default:
                // Log::info("instructBack--------");
                // Log::info($message);
                // Log::info("instructBack--------");
                break;
        }
        return "success";
    }

    public function dataBack(Request $request)
    {
        if (isset($request->echostr)) {
            return WorkService::msgDecryptCorp($request);
        }
        $message = WorkService::msgDecryptKefu($request);
        if($message["MsgType"] == "error") return "error";
        switch ($message['MsgType']) {
            case 'event':
                if ($message['Event'] == "kf_msg_or_event") {
                    try {
                        $corp_id = $message['ToUserName'];
                        WorkkefuService::syncMsgLists($message['ToUserName'],$message['Token']);
                    } catch (Exception $e) {
                        Log::info("dataBack---------");
                        Log::info($e);
                        Log::info("dataBack---------");
                    }
                } elseif($message['Event'] == "change_app_admin"){
                    // 应用管理员变更通知
                } else {
                    // Log::info("dataBack-------1");
                    // Log::info($request->all());
                    // Log::info(json_encode($message));
                    // Log::info("dataBack-------1");
                }
                break;
            default:
                // Log::info("dataBack-------2");
                // Log::info($message);
                // Log::info("dataBack-------2");
                break;
        }
        return "success";
    }

    public function interBack(Request $request)
    {
        if (isset($request->echostr)) {
            return WorkService::msgDecryptCorp($request);
        }
        $message = WorkService::msgDecryptInter($request);
        if($message["InfoType"] == "error") return "error";
        switch ($message['InfoType']) {
            case 'license_pay_success':
                // 互通账号购买成功
                WorkhanddleService::licensePaySuccess($message);
                break;
            case 'auto_activate':
                // 自动激活回调通知
                WorkhanddleService::licenseAutoActivate($message);
                break;
            case 'pay_for_app_success':
                // 购买软件推送
                WorkhanddleService::payForAppSuccess($message);
                break;
            default:
                Log::info("interBack--------");
                Log::info(json_encode($message));
                Log::info("interBack--------");
                break;
        }
        return "success";
    }
}
